package com.whisperly.echotalk.bllmodel;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.whisperly.echotalk.starryb.R;

import java.util.List;

public class ChatContentAdapter extends BaseAdapter {

    public List<ChatWithUser> chatWithUserList;
    private Context context;
    private OnMessageLongClickListener longClickListener;

    public ChatContentAdapter(Context context, List<ChatWithUser> chatBeanList,OnMessageLongClickListener longClickListener) {
        this.context = context;
        this.chatWithUserList = chatBeanList;
        this.longClickListener = longClickListener;
    }

    @Override
    public int getCount() {
        return chatWithUserList.size();
    }

    @Override
    public Object getItem(int position) {
        return chatWithUserList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();

        if (chatWithUserList.get(position).getState() == ChatWithUser.RECEIVE) {

            contentView = View.inflate(context, R.layout.chat_left, null);
        } else {

            contentView = View.inflate(context,R.layout.chat_right,null);
        }

        ChatWithUser chatWithUser = chatWithUserList.get(position);

        holder.tvContent = (TextView) contentView.findViewById(R.id. tv_chat_content);
        holder.tvContent.setText(chatWithUserList.get(position).getMessage());

        contentView.setOnLongClickListener(v -> {
            if (longClickListener != null) {
                longClickListener.onMessageLongClick(chatWithUser);
            }
            return true;
        });

        return contentView;
    }

    public interface OnMessageLongClickListener {
        void onMessageLongClick(ChatWithUser message);
    }

    static class ViewHolder{
        TextView tvContent;
    }
}
